# @time     :2021/12/31
# @Author   :Simple
# @FileName :login_page
from time import sleep

from selenium.common.exceptions import NoAlertPresentException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.element import GetElement
from qyex.page.admin_page import AdminPage
from qyex.page.register_page.register_page import RegisterPage


class LoginPage:
    register_btn = GetElement(xpath="//*[@class='login_registerBar_link']", describe="注册按钮", wait=True)
    menu_btn = GetElement(xpath="//a[@class='index_info_name']", describe="首页按钮", wait=True)

    def __init__(self, driver):
        self.driver = driver

    def goto_register(self):
        self.register_btn.click()
        return RegisterPage(self.driver)

    def scan(self):
        """
        等待手动扫码登录
        :return:
        """
        self.driver.execute_script("window.alert('等待15s登录。。')")
        sleep(2)
        try:
            self.driver.switch_to.alert.accept()
        except NoAlertPresentException:
            pass
        try:
            # by = (By.XPATH, "//a[@class='index_info_name']")
            # menu_btn = WebDriverWait(self.driver, 15).until(expected_conditions.presence_of_element_located(by))
            print("登录成功, ", self.menu_btn.text)
            if self.menu_btn:
                return AdminPage(self.driver)
        except TimeoutException:
            return None
